package br.edu.ifrn.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;


import br.edu.ifrn.controller.DocumentoEJBLocal;
import br.edu.ifrn.model.Documento;

@ManagedBean
@ViewScoped
public class BaixaMBean {
	private List<SelectItem> selectedItems;
	private List<Documento> documentos;
	private Map<Long, Boolean> mapaItens;
	@EJB
	private DocumentoEJBLocal documentoEJB;
	
	public BaixaMBean() {
		selectedItems = new ArrayList<SelectItem>();
		mapaItens = new HashMap<Long, Boolean>();
	}
	
	@PostConstruct
	public void construct() {
		documentos = documentoEJB.listar();
	}
	
	public String index(){
		return "/documento/darbaixa.xhtml";
	}
	public String descartar(){
		for (Object id : mapaItens.keySet().toArray()) {
			if(mapaItens.get(id)){
				Documento documento = documentoEJB.find((Long)id);
				if (documento.isPassivelDescarte()) {
					documento.setDescartado(true);
					documentoEJB.salvar(documento);
					
				}
			}
		}
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO,
						"Documento(s) descartado(s)", null));
		return index();
	}
	
	public List<SelectItem> getSelectedItems() {
		return selectedItems;
	}

	public void setSelectedItems(List<SelectItem> selectedItems) {
		this.selectedItems = selectedItems;
	}

	public List<Documento> getDocumentos() {
		return documentos;
	}

	public void setDocumentos(List<Documento> documentos) {
		this.documentos = documentos;
	}

	public Map<Long, Boolean> getMapaItens() {
		return mapaItens;
	}

	public void setMapaItens(HashMap<Long, Boolean> mapaItens) {
		this.mapaItens = mapaItens;
	}
}
